
define(['ionic', 'controllers'], function () {
    angular.module('U9Mobile.controllers')
        .controller('ReListCtrl', ['$scope', '$ionicHistory', '$ionicSlideBoxDelegate', 'AppManager', 'AppConstants',
            function ($scope, $ionicHistory, $ionicSlideBoxDelegate, AppManager, AppConstants) {
                $scope.data = {
                    appsArray: [],
                    temp: [],
                    activeApp: ''
                };

                var reListSlideBox = $ionicSlideBoxDelegate.$getByHandle('relistapps'),
                    preIndex = 0;

                AppManager.loadingAppPromise.then(function () {
                    $scope.data.appsArray = formatApps(AppManager.getInstallApps());
                });

                $scope.activeApp = function (appId) {
                    $scope.data.activeApp = appId;
                };
                $scope.unActiveApp = function () {
                    $scope.data.activeApp = '';
                };
                $scope.onDragStart = function () {
                    reListSlideBox.enableSlide(false);
                    preIndex = reListSlideBox.currentIndex();
                };
                $scope.onDrag = function(event, ui) {
                    var posX = event.clientX,
                        clientWidth = document.body.clientWidth,
                        cIndex = reListSlideBox.currentIndex();
                    ui.position.left += clientWidth * (cIndex - preIndex);
                    if (posX < 20) {
                        reListSlideBox.previous();
                        return;
                    }
                    if (clientWidth - posX < 20) {
                        reListSlideBox.next();
                        return;
                    }
                };
                $scope.onDragStop = function () {
                    reListSlideBox.enableSlide(true);
                };
                $scope.submit = function () {
                    var temps = angular.copy($scope.data.appsArray),
                        apps = [];
                    angular.forEach(temps, function (appArray) {
                        angular.forEach(appArray, function (app) {
                            delete app.$$hashKey;
                            apps.push(app);
                        });
                    });
                    AppManager.setInstallApps(apps).then(function () {
                        $ionicHistory.goBack();
                    });
                };

                function formatApps (apps) {
                    var maxNum = AppConstants.APP_NUM_PER_PAGE,
                        j = 0,
                        temp = [],
                        results = [];
                    for (var i in apps) {
                        temp.push(apps[i]);
                        j++;
                        if (j === maxNum) {
                            j = 0;
                            results.push(temp);
                            temp = [];
                        }
                    }
                    if (apps.length % maxNum !== 0) {
                        results.push(temp);
                    }
                    return results;
                }
            }
        ]);
});